package org.example.mallchat.common.thread;

import java.util.concurrent.ThreadFactory;

/**
 * @author: zzh
 * @date: 2024/12/20 14:17:45
 * @version: 1.0
 * @desc: 自定义线程工厂 装饰器模式
 */
public class MyThreadFactory implements ThreadFactory {

    private static final MyUncaughtExceptionHandler MY_UNCAUGHT_EXCEPTION_HANDLER = new MyUncaughtExceptionHandler();
    private ThreadFactory original;

    public MyThreadFactory(ThreadFactory original) {
        this.original = original;
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread thread = original.newThread(r);
        // 装饰线程
        thread.setUncaughtExceptionHandler(MY_UNCAUGHT_EXCEPTION_HANDLER);
        return thread;
    }
}
